<template>
  <div class="personal-main">
    <div class="pmain-profile">
      <div class="pmain-welcome">
        <span class="fr">上次登录时间： {{ userIndexVO.lastLoginTime }} </span>
      </div>
      <div class="pmain-user">
        <div class="user-head">
          <span class="head-img">
            <span>
              <img
                :src="userIndexVO.headImg"
                style="width:88px;height:88px;z-index:0;"
              />
              <i class="headframe" style="z-index:0;"></i>
            </span>
          </span>
        </div>
        <div class="user-info">
          <ul>
            <li>
              用户名<span>{{ userIndexVO.name }}</span>
              <NuxtLink
                v-if="
                  userIndexVO.userType === 2 && userIndexVO.bindStatus === 1
                "
                to="/user/borrower"
              >
                立即借款
              </NuxtLink>
            </li>
            <li v-if="userIndexVO.bindStatus !== 1">
              您还未开通第三方支付账户，请
              <NuxtLink to="/user/bind">立即开通</NuxtLink>
              以确保您的正常使用和资金安全。
            </li>
          </ul>
        </div>
      </div>

      <div v-if="userIndexVO.bindStatus === 1" class="pmain-money">
        <ul>
          <li class="none">
            <span>
              <em>账户余额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.amount }}</i> 元
            </span>
          </li>
          <li>
            <span>
              <em>冻结金额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.freezeAmount }}</i
              >元
            </span>
          </li>
          <li>
            <NuxtLink to="/user/recharge">
              <el-button size="mini" type="danger">充值</el-button>
            </NuxtLink>
            <NuxtLink to="/user/withdraw">
              <el-button size="mini" type="success">提现</el-button>
            </NuxtLink>
          </li>
        </ul>
      </div>
    </div>

    <!-- wx用户绑定个人数据的对话框 -->
    <el-dialog
      title="完善个人信息"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <el-form :model="userInfo">
        <el-form-item label="手机号码" :label-width="formLabelWidth">
          <el-input v-model="userInfo.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" :label-width="formLabelWidth">
          <el-select v-model="userInfo.userType" placeholder="请选择类型">
            <el-option label="借款人" value="2"></el-option>
            <el-option label="投资人" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUserInfoBind" :disabled="flag"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      flag: false,
      userInfo: {},
      formLabelWidth: "120px",
      dialogFormVisible: false,
      userIndexVO: {},
    };
  },
  //使用mounted查询数据：因为需要使用localStorage
  mounted() {
    this.fetchUserData();
  },

  methods: {
    //完善数据对话框提交按钮事件函数
    handleUserInfoBind() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.warning("请先登录");
        return;
      }
      //禁用提交按钮
      this.flag = true;
      //console.log(this.userInfo);
      this.$axios({
        //baseURL:'http://虚拟机ip'
        url: `/api/core/userInfo/update/${this.userInfo.mobile}/${this.userInfo.userType}`,
        method: "PUT",
        headers: { token },
      }).then((r) => {
        //更新成功
        //先删除之前的SRB-USER缓存：他的userType和mobile已经被更新了
        localStorage.removeItem("SRB-USER");
        //如果后端更新后返回了更新过的userInfo对象，前端可以获取直接写入到localStorage中替换之前的
        //  r.data.item
        //重新加载整个页面 包括页头
        window.location.reload();
      });
    },
    fetchUserData() {
      //查询个人中心首页数据前先判断用户数据是否已经完善，未完善弹出对话框完善数据
      let srbUser = localStorage.getItem("SRB-USER");
      if (srbUser) {
        srbUser = JSON.parse(srbUser);
        // usertype=1 , mobile
        if (
          (srbUser.userType != 1 && srbUser.userType != 2) ||
          !srbUser.mobile
        ) {
          this.dialogFormVisible = true;
          return;
        }
      }
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.warning("请先登录");
        return;
      }
      //查询会员中心首页数据展示
      this.$axios({
        url: "/api/core/userInfo/auth/userCenterIndexVO",
        method: "GET",
        headers: { token }, //创建json对象 属性名为token字面量 值为token值
      }).then((r) => {
        this.userIndexVO = r.data.data.item;
      });
    },
  },
};
</script>
